const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    adviserNoRead:0,
    isLogin:1,
    isPower: '',
    isMain:false,
    StatusBar: app.globalData.StatusBar,
    CustomBar: app.globalData.CustomBar,
    userInfo: {},
    user_changeInfo: null,
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    isMobile:'400-887-1216',
    isDuaration: true,
    iconList: [
      {
        icon: 'favorfill',
        color: 'red',
        badge: 120,
        name: '我的收藏',
        url: '/pages/member/myfavorite/index'
      },
      // {
      //   icon: 'friendfamous',
      //   color: 'blue',
      //   badge: 1,
      //   name: '经纪人入驻',
      //   url: '/pages/register/index'
      // },
      // {
      //   icon: 'profile',
      //   color: 'red',
      //   badge: 1,
      //   name: '置业顾问入驻',
      //   url: '/pages/readviser/index',
      // },
      // {
      //   icon: 'picfill',
      //   color: 'yellow',
      //   badge: 0,
      //   name: '我的名片',
      //     url: '/pages/idcard/index'
      // },
      // {
      //     icon: 'dianhua',
      //     color: 'olive',
      //     badge: 22,
      //     name: '联系客服',
      //     isMobile: '400-887-1216',
      // },
      // {
      //     icon: 'writefill',
      //     color: 'cyan',
      //     badge: 0,
      //     name: '有问必答',
      //     url:"/pages/member/suggest/index"
      // },
      {
        icon: 'messagefill',
        color: 'orange',
        badge: 0,
        name: '我的房友圈',
        url: '/pages/mywrite/index'
      },

      {
        icon: 'comment',
        color: 'yellow',
        badge: 0,
        name: '我的消息',
        url: '/pages/adviser/userreply/index'
      },
      {
        icon: 'weixin',
        color: 'green',
        badge: 0,
        name: '官方公众号',
        url: '/pages/official/index'
      },
      // {
      //   icon: 'moneybag',
      //   color: 'orange',
      //   badge: 0,
      //   name: '贷款预算',
      //   url: '/pages/computer/index/index'
      // },
      // {
      //   icon: 'home',
      //   color: 'red',
      //   badge: 0,
      //   name: '在线订房',
      //   url: '/pages/loupan/zaixianlist'
      // },
      {
        icon: 'settings',
        color: 'cyan',
        badge: 0,
        name: '设置',
        url: '/pages/member/settings/index'
      },
      {
        icon: 'infofill',
        color: 'blue',
        badge: 0,
        name: '关于我们',
        url: '/pages/member/aboutus/index'
      },
      {
        icon: 'favorfill',
        color: 'red',
        badge: 120,
        name: '我的订房',
        url: '/pages/newhouse/myorder/index'
      },
      {
        icon: 'favorfill',
        color: 'red',
        badge: 120,
        name: '我的二手房',
        url: '/pages/oldhouse/mypost/index'
      },
      {
        icon: 'favorfill',
        color: 'red',
        badge: 120,
        name: '我的租房',
        url: '/pages/hire/mypost/index'
      },
    ],

    gridCol: 3,
    isAdviser: 0,
    isAgent: -1,
    agentList: [],
    user_id: null,
    oldhouseList: [

    ],
    redirect: null
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options.redirect)
    if (options.redirect != undefined && options.redirect != '') {
      this.setData({
        redirect: decodeURIComponent(options.redirect)
      })
    }
    setTimeout(() => {
      this.setData({
        isDuaration: false,
      })
    }, 1000)
    // console.log(app.globalData.userInfo.avatarUrl)
    app.core('get','getCashMoneyAgent','',(res) => {
      this.setData({
        agentList: res.data
      })
    })
  },

  getUserInfo: function (e) {
    console.log(e)
    var vm = this;
    var UserInfo = e.detail.userInfo
    // wx.setStorageSync('userInfo', e.detail.userInfo)
    wx.login({
      success(res) {
        console.log(res)
        if (res.code) {
          var userCode = res.code;
          var referrer = wx.getStorageSync('referrer')
          app.core('post', 'create_xcx_user', {
                'nickname': UserInfo.nickName,
                'headpic': UserInfo.avatarUrl,
                'js_code': userCode,
                'referrer': referrer
              }, (res) => {
                console.log(res)
                // return false
                console.log("open_id" + res.data.open_id);
                wx.setStorageSync('userInfo', e.detail.userInfo);
                console.log(wx.getStorageSync('userInfo'));
                wx.setStorageSync('user_new', res.data.open_id);
                wx.setStorageSync('sessionKey', res.sessionKey);
                console.log(wx.getStorageSync('user_new'))

                getApp().globalData.openid = res.data.open_id

                vm.setData({
                  userInfo: e.detail.userInfo,
                  hasUserInfo: true,
                  isMain: true,
                  isPower: wx.getStorageSync('user_new')
                });
                wx.hideLoading()

                console.log(vm.data.redirect)
                if (vm.data.redirect != undefined && vm.data.redirect != '') {
                  wx.navigateTo({
                    url: vm.data.redirect,
                  })
                } else {
                  wx.reLaunch({
                    url: '/pages/member/index',
                  })
                }
              },
              (err) => {
                console.log(err)
              }
          )

        } else {
          console.log('登录失败！' + res.errMsg)
        }
      }
    });


  },

  // wx.setStorageSync('user_info', e.detail.userInfo);
  // console.log(wx.getStorageSync('user_info'));
  showModal(e) {
    this.setData({
      modalName: e.currentTarget.dataset.target
    })
  },
  hideModal(e) {
    this.setData({
      modalName: null
    })
  },
  gotoItem:function (e) {
    if (e.currentTarget.dataset.mobile){
      var mobile = e.currentTarget.dataset.mobile;
      wx.showModal({
        title: '系统提示',
        content: '确定要联系 ' + mobile + ' 号码吗',
        success(res) {
          if (res.confirm) {
            wx.makePhoneCall({
              phoneNumber: mobile
            })
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    } else{
      wx.navigateTo({
        url: e.currentTarget.dataset.url
      });
    }

  },

  toagent() {
    wx.navigateTo({
      url: '/pages/register/index',
    })
  },
  toadviser() {
    wx.navigateTo({
      url: '/pages/readviser/index',
    })
  },
  tocenter() {
    wx.navigateTo({
      url: '/pages/salecenter/index',
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // console.log(app.globalData.userInfo)
    let userInfo = wx.getStorageSync('userInfo')
    let user_new = wx.getStorageSync('user_new')

    console.log(userInfo,user_new)
    if (userInfo) {
      this.setData({
        userInfo: userInfo,
        hasUserInfo: true,
        isMain: true,
        isPower: wx.getStorageSync('user_new'),
      })
    } else if (this.data.canIUse) {
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      this.setData({
        isPower:null
      })
      // wx.getUserInfo({
      //   success: res => {
      //     app.globalData.userInfo = res.userInfo
      //     this.setData({
      //       userInfo: res.userInfo,
      //       hasUserInfo: true
      //     })
      //   }
      // })
    }
    let open_id = wx.getStorageSync('user_new')
    if (open_id == ''){
      return false
    }
    app.core('get', 'userInfo?open_id=' + open_id, '', (res) => {
      console.log(res)
      this.setData({
        user_changeInfo: res.data
      })
    })
    app.core('get', 'getUserInfo?open_id=' + open_id, '', (res) => {
      console.log(res,1)
      this.setData({
        user_id: res.data.user_id,
        isAdviser: res.data.adviser_id,
        adviserNoRead: res.data.adviserNoRead
      })

      wx.setStorageSync('user', res.data)
    })
    app.core('get', 'findAgentByOpenid?open_id=' + open_id, '', (res) => {
      // console.log(res)
      let resultCode = res.resultCode
      if (resultCode === 500) {
        this.setData({
          isAgent: 0,
          adviserInfo: res.adviser,
        })
        return true
      }
      if (resultCode === 200) {
        let status = res.data.status
        this.setData({
          isAgent: status,
          adviserInfo: res.adviser,
        })
      }
    })
  },
  // 清楚缓存
  clearStroage() {
    try {
      wx.removeStorageSync('userInfo')
      wx.removeStorageSync('user_new')
      wx.removeStorageSync('index.adv')
      getApp().globalData.openid = ''
      wx.reLaunch({
        url: '/pages/member/index',
      })
    } catch (e) {
      // Do something when catch error
    }
  },

  // 经纪人菜单
  tocustomer() {
    wx.navigateTo({
      url: '/pages/agent/index',
    })
  },
  tomycust() {
    wx.navigateTo({
      url: '/pages/agent/mycust/index',
    })
  },
  toMymoney() {
    wx.navigateTo({
      url: '/pages/agent/mymoney/index',
    })
  },
  // 置业顾问留言板
  tomessage() {
    wx.navigateTo({
      url: '/pages/adviser/reply/reply',
    })
  },
  // 追加楼盘
  toaddbuilding() {
    wx.navigateTo({
      url: '/pages/readviser/addbuilding/addbuilding',
    })
  },
  // 我的楼盘
  toMybuilding() {
    let adviser_id = this.data.isAdviser
    wx.navigateTo({
      url: `/pages/adviser/mybuilding/mybuilding?adviser_id=${adviser_id}`,
    })
  },


  toadvisercustomer() {
    wx.navigateTo({
      url: '/pages/adviser/report/report',
    })
  },
  toadvisermycust() {
    wx.navigateTo({
      url: '/pages/adviser/mycust/mycust',
    })
  },
  toadviserMymoney() {
    wx.navigateTo({
      url: '/pages/adviser/mymoney/mymoney',
    })
  },


  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (e) {
    wx.showShareMenu({
      withShareTicket: true
    })

  },

  // 自定义方法集合
  menuArrow: function (e) {
    this.setData({
      menuArrow: e.detail.value
    });
  },
  menuBorder: function (e) {
    this.setData({
      menuBorder: e.detail.value
    });
  },
  menuCard: function (e) {
    this.setData({
      menuCard: e.detail.value
    });
  },

  aiCard() {
    const user_id = this.data.user_id;
    if (user_id) {
      wx.navigateTo({
        url: '/pages/aiCard/index?user_id=' + user_id,
      })
    }
  },

  toFollow() {
    const user_id = this.data.user_id;
    if (user_id) {
      wx.navigateTo({
        url: '/pages/member/follow/index?user_id=' + user_id,
      })
    }
  },

})
